package hw.pjbk;

import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

//class for the activity_history_questions activity
public class HistoryQuestions extends Activity implements OnClickListener {

	private TextView textViewQuestion;
	private TextView textViewTotals;
	private DatabaseHelper db;
	private Button nextButton;
	private Cursor questions;
	private RadioGroup radioButtons;
	private int numCorrectAnswers = 0;
	private int numQuestionsAttempted = 0;
	private int correctAnswerID;
	private Quiz1Application application;
	private int noQuestions;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history_questions);

		application = (Quiz1Application) this.getApplication();

		radioButtons = (RadioGroup) findViewById(R.id.radio_history_group);

		textViewQuestion = (TextView) findViewById(R.id.textview_history_question);

		textViewTotals = (TextView) findViewById(R.id.textview_history_totals);

		nextButton = (Button) findViewById(R.id.history_next_button);
		nextButton.setOnClickListener(this);		
		
		// get the number of questions specified in the app preferences
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());
		noQuestions = Integer.parseInt(pref.getString("noofquestions", "1"));		
		
		db = new DatabaseHelper(this);
		questions = db.getRandomQuestions(noQuestions);
		questions.moveToFirst();

		// run the private method to display the question and answers
		setQandA();

	}

	// display the question text and answer radio buttons
	private void setQandA() {

		Log.i("HIST", "Setting question text");
		// set the question text
		textViewQuestion.setText(questions.getString(questions
				.getColumnIndexOrThrow("QuestionText")));

		// set the variable correctAnswerID to the correct answer for this
		// question
		correctAnswerID = Integer.parseInt(questions.getString(questions
				.getColumnIndexOrThrow("CorrectAnswerID")));

		Log.i("HIST", "Getting answers for question ");
		// get all possible answers for this question
		String questionID = questions.getString(questions
				.getColumnIndexOrThrow("_id"));
		Cursor answers = db.getAnswers(questionID);

		Log.i("HIST", "Creating answers for question " + questionID);
		// set the radio buttons to match the possible answers
		radioButtons.removeAllViews();
		answers.moveToFirst();
		while (answers.isAfterLast() == false) {
			Log.i("HIST", "Creating radio button");
			RadioButton r = new RadioButton(this);
			r.setText(answers.getString(answers
					.getColumnIndexOrThrow("AnswerText")));
			Integer buttonId = Integer.parseInt(answers.getString(answers
					.getColumnIndexOrThrow("_id")));
			r.setId(buttonId);
			Log.i("HIST", "Radio button with ID " + buttonId + " created");
			radioButtons.addView(r);
			answers.moveToNext();

		}

	}

	// handle the click event of the button
	@Override
	public void onClick(View v) {
		if (v == nextButton) {
			Log.i("HIST", "Next > button clicked");
			// check to make sure user made a selection
			int checkedRadioButtonID = radioButtons.getCheckedRadioButtonId();
			if (checkedRadioButtonID == -1) {
				Log.i("HIST", "No radio button checked");
				// if no selection remind user with some toast
				Toast toast = Toast.makeText(getApplicationContext(),
						"Please select an answer", Toast.LENGTH_SHORT);
				toast.show();

			} else {
				// user made a selection
				application.incrementQuestionsAttempted();
				numQuestionsAttempted++;

				Log.i("HIST", "Radio button with ID " + checkedRadioButtonID
						+ " checked");
				// user got the question correct
				if (checkedRadioButtonID == correctAnswerID) {
					Toast toast = Toast.makeText(getApplicationContext(),
							"Correct answer!", Toast.LENGTH_SHORT);
					toast.show();
					application.incrementQuestionsCorrect();
					numCorrectAnswers++;
					// wrong answer
				} else {
					Toast toast = Toast.makeText(getApplicationContext(),
							"Sorry, wrong answer!", Toast.LENGTH_SHORT);
					toast.show();

				}
				// update the totals label
				textViewTotals.setText(numCorrectAnswers
						+ " correct answers out of " + numQuestionsAttempted
						+ " questions attempted");

				if (questions.isLast() == false) {
					questions.moveToNext();
					setQandA();
				} else {
					finish();
				}
			}

		}

	}

}
